<template>
  <div class="app-container">
  <!-- 搜索区 -->
    <el-form :model="queryParam" ref="queryForm" size="small" :inline="true">
      <el-form-item label="字典名称" prop="dictType">
       <el-select v-model="queryParam.dictType" placeholder="请选择" @change="dictTypeChange"  clearable>
          <el-option
            v-for="item in dictTypeList"
            :key="item.id"
            :label="item.dictName"
            :value="item.id">
          </el-option>
        </el-select>

      </el-form-item>

      <el-form-item label="字典标签" prop="dictLabel">
        <el-input v-model="queryParam.dictLabel" placeholder="字典标签" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item label="状态" prop="status">
         <el-select v-model="queryParam.status" placeholder="请选择"  clearable>
            <el-option
              v-for="item in dicts.sys_normal_disable"
              :key="item.id"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="warning" icon="el-icon-close" size="mini" @click="handleClose">关闭</el-button>
      </el-form-item>
    </el-form>


     <!--功能区-->
        <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd()"
                >新增</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="success"
                  plain
                  icon="el-icon-edit"
                  size="mini"
                  :disabled="single"
                  @click="handleUpdate()"
                >修改</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  :disabled="multiple"
                  @click="handleDelete()"
                >删除</el-button>
              </el-col>

        </el-row>

    <el-table :data="tableData.rows" border style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="id" label="编码" width="80" />
      <el-table-column prop="dictLabel" label="字典标签"   />
      <el-table-column prop="dictValue" label="字典键值" />
      <el-table-column prop="status" label="字典排序" />

      <el-table-column label="状态" width="180">
        <template slot-scope="scope">
          <DictTag :dataArray="dicts.sys_normal_disable" :value="scope.row.status"></DictTag>
        </template>
      </el-table-column>
       <el-table-column prop="remark" label="备注" width="180"  />
      <el-table-column prop="createTime" label="创建时间" width="180" />

      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <el-pagination v-if="tableData.total>0" :current-page="queryParam.page" :page-sizes="[1, 5, 10, 20,30,50,100]"
      :page-size="queryParam.size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />

     <!-- 添加或修改参数配置对话框 -->
       <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false" :before-close="cancelForm">
         <el-form ref="form" :model="form"  label-width="80px">
            <el-form-item label="字典类型" :rules="[  { required: true, message: '该值不能为空', trigger: 'blur' }]">
              <el-input v-model="form.dictType" :disabled="true" />
            </el-form-item>
            <el-form-item label="数据标签" prop="dictLabel" :rules="[  { required: true, message: '该值不能为空', trigger: 'blur' }]">
              <el-input v-model="form.dictLabel" placeholder="请输入数据标签" />
            </el-form-item>
            <el-form-item label="数据键值" prop="dictValue" :rules="[  { required: true, message: '该值不能为空', trigger: 'blur' }]">
              <el-input v-model="form.dictValue" placeholder="请输入数据键值" />
            </el-form-item>
            <el-form-item label="样式属性" prop="cssClass">
              <el-input v-model="form.cssClass" placeholder="请输入样式属性" />
            </el-form-item>

            <el-form-item label="显示排序" prop="dictSort" :rules="[  { required: true, message: '该值不能为空', trigger: 'blur' }]">
              <el-input-number v-model="form.dictSort" controls-position="right" :min="0" />
            </el-form-item>

            <el-form-item label="回显样式" prop="listClass">
              <el-select v-model="form.listClass">
                <el-option
                  v-for="item in listClassOptions"
                  :key="item.value"
                  :label="item.label + '(' + item.value + ')'"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>

           <el-form-item label="状态" prop="status" :rules="[  { required: true, message: '该值不能为空', trigger: 'blur' }]">
             <el-radio-group v-model="form.status">
               <el-radio
                 v-for="dict in dicts.sys_normal_disable"
                 :key="dict.dictId"
                 :label="dict.dictValue"
               >{{dict.dictLabel}}</el-radio>
             </el-radio-group>
           </el-form-item>

            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
           <el-button type="primary" @click="submitForm">确 定</el-button>
           <el-button @click="cancelForm">取 消</el-button>
          </div>
        </el-dialog>

  </div>
</template>

<script>
  import DictMixin from '@/mixins/DictMixin.js'
  import PageMixin from '@/mixins/PageMixin.js'
  import AddOrEditMixin from '@/mixins/AddOrEditMixin.js'
  import {dataAllAPI} from '@/api/dicttype.js'
  import {dataApi, addApi, editApi, findByIdApi,deleteByIdApi} from '@/api/dictdata.js'

  export default {
    dicts: ['sys_normal_disable'],
    mixins: [DictMixin, PageMixin,AddOrEditMixin],
    data(){
      return {
        // 所有字典明细
        dictTypeList: [],
        // 查询函数
        fetchDataApi: dataApi,
        // 新增保存函数
        addApi: addApi,
         // 修改保存函数
        editApi: editApi,
         // 根据主键查询函数
        findByIdApi: findByIdApi,
        // 逻辑删除
        deleteByIdApi:deleteByIdApi,
        primaryKey: 'id',
        allowSearch: false,
        // 数据标签回显样式
        listClassOptions: [
          {
            value: "default",
            label: "默认"
          },
          {
            value: "primary",
            label: "主要"
          },
          {
            value: "success",
            label: "成功"
          },
          {
            value: "info",
            label: "信息"
          },
          {
            value: "warning",
            label: "警告"
          },
          {
            value: "danger",
            label: "危险"
          }
        ]
      }
    },
    methods: {
      //加载所有的数据字典
       fetchAllDicType(){
         dataAllAPI().then(resp=> {
           this.dictTypeList= resp.data

         })
       },
       //数据字典发生变化
       dictTypeChange(id){
         this.$set(this.queryParam,'dictType', id)
         this.$set(this.queryParam,'dictLabel', undefined)
         this.$set(this.queryParam,'status', undefined)
         this.queryParam.page=1
         this.fetchData()
         this.formDictTypeSetValue(id)
       },
       //关闭
       handleClose(){
         this.$router.push('/dict/type')
       },
       // 重置表单
       initFormValue(){
         this.form= {
           status: '0',
           listClass: 'default' 
         }

         this.formDictTypeSetValue(parseInt(this.queryParam.dictType))
       },
       formDictTypeSetValue(id){
         //为form新增表单赋值
         console.log(id)
         const arr=this.dictTypeList.filter(p=> p.id===id);
         if(arr.length>0){
           this.$set(this.form,'dictType', arr[0].dictType)
         }else{
           this.$set(this.form,'dictType', undefined)
         }
       }
    },
    created(){
      this.initFormValue()

      this.fetchAllDicType()
      const params=this.$route.params;
      const id= params.id
      //调用根据sys_dict_type  的id查询 sys_dict_data数据方法
      //https://v2.cn.vuejs.org/v2/api/#Vue-set
      this.$set(this.queryParam,'dictType', parseInt(id))
      //this.queryParam.dictType=parseInt(id)
      this.fetchData()

      //为form新增表单赋值
      setTimeout(()=> {
        this.formDictTypeSetValue(this.queryParam.dictType)
      },500)

    }
  }
</script>

<style>
</style>
